package com.demo.forum.controller;

import com.demo.forum.common.Result;
import com.demo.forum.common.ResultCode;
import com.demo.forum.config.AppConfig;
import com.demo.forum.model.Message;
import com.demo.forum.model.User;
import com.demo.forum.services.IMessageService;
import com.demo.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "站内信接口")
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IMessageService messageService;

    @ApiOperation("发送站内信")
    @PostMapping("/send")

    public Result send(HttpServletRequest request,
                      @ApiParam("接收方id")@RequestParam("receiveUserId") @NonNull Long receiveUserId,
                      @ApiParam("内容") @RequestParam("content") @NonNull  String content){
        //校验当前登录用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_LOGUN);
        if(user.getState() == 1){
            //用户已经被禁言
            return Result.failed(ResultCode.FAILED_USER_BANNED);

        }
        //用户不能给自己发消息
        if(user.getId() == receiveUserId){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);

        }
        //判断接受方状态
        User receiveUser  = userService.selectById(receiveUserId);
        if(receiveUser == null || receiveUser.getDeleteState() ==1 ){
            return Result.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        //构造对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setContent(content);
        message.setReceiveUserId(receiveUserId);
        messageService.create(message);
        return Result.success("发送成功!");
    }


    @ApiOperation("获取未读数")
    @GetMapping("/getUnreadCount")
    public Result getUnreadCount(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_LOGUN);
        Integer count = messageService.selectUnreadCount(user.getId());
        return Result.success(count);

    }
    @ApiOperation("查询用户的所有站内信")
    @GetMapping("/getAll")
    public Result<List<Message>> getAll (HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_LOGUN);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return Result.success(messages);

    }

    @ApiOperation("更新为已读")
    @PostMapping("/markRead")
    public Result markRead (HttpServletRequest request,
                               @ApiParam("站内信Id") @RequestParam("id") @NonNull Long id) {



        //判断帖子状态 ?
        Message message = messageService.selectById(id);
        if(message == null || message.getDeleteState() == 1){
            return Result.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        //判断当前登录用户和帖子接受者是否相同
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_LOGUN);

        if(user.getId() != message.getReceiveUserId()){
            return Result.failed(ResultCode.FAILED_FORBIDDEN);
        }

        messageService.updateStateById(id,(byte)1);
        return Result.success();


    }

    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public Result reply (HttpServletRequest request,
                            @ApiParam("要回复的站内信Id") @RequestParam("repliedId") @NonNull Long repliedId,
                            @ApiParam("站内信的内容") @RequestParam("content") @NonNull String content) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_LOGUN);
        //校验用户是否被禁言
        if(user.getState() ==1){
            return Result.failed(ResultCode.FAILED_USER_BANNED);
        }
        //判断要回复的站内信是否存在
        Message existsMessage = messageService.selectById(repliedId);
        if(existsMessage == null || existsMessage.getDeleteState() == 1){
            return Result.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //自己不能给自己回复
        if(user.getId() == existsMessage.getPostUserId()){
            return Result.failed("自己不能给自己回复");
        }
        Message message = new Message();
        message.setPostUserId(existsMessage.getReceiveUserId());
        message.setReceiveUserId(existsMessage.getPostUserId());
        message.setContent(content);
        messageService.reply(repliedId,message);
        return Result.success();



    }


}
